package com.myblog.web.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myblog.Mapper.Tagmapper;
import com.myblog.Mapper.typemapper;

import com.myblog.po.Blog;
import com.myblog.po.Type;
import com.myblog.po.User;
import com.myblog.service.BlogService;
import com.myblog.vo.BlogQuery;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;

    @Autowired
    private typemapper typeService;
    @Autowired
    private Tagmapper tagService;

    @GetMapping("/blogs")
    public String blogs(@RequestParam(value = "page",required = false) Integer page, Model model){

        if(page == null){
            page = 1;
        }

        PageHelper.startPage(page,5);
        List<Blog> blogs = blogService.listblog();

        for (Blog blog: blogs) {
            blog.setType(typeService.getType(blog.getType_id()));
        }


        PageInfo pageInfo = new PageInfo(blogs);

        model.addAttribute("types",typeService.listtype());
        model.addAttribute("page" ,pageInfo );
        return "admin/blogs";
    }




    @PostMapping("/blogs/search")
    public String search(@RequestParam(value = "page",required = false) Integer page, BlogQuery blog, Model model){

        if(page == null){
            page = 1;
        }

        PageHelper.startPage(page,5);


        List<Blog> blogs = blogService.searchblog(blog);
        for (Blog blogg: blogs) {
            blogg.setType(typeService.getType(blogg.getType_id()));
        }
        PageInfo pageInfo = new PageInfo(blogs);

        model.addAttribute("page" , pageInfo );
        return "admin/blogs::blogList";

    }

        @GetMapping("/blogs/input")
        public String input(Model model)
        {
            Blog blog = new Blog();
            blog.setType(new Type());

            model.addAttribute("blogs",blog);
          //  model.addAttribute("tags",tagService.listTag());
            model.addAttribute("types",typeService.listtype());

            return "admin/blogs-input";
        }

        private void setTypeAndTag(Model model){
            model.addAttribute("tags",tagService.listTag());
            model.addAttribute("types",typeService.listtype());

        }


        @GetMapping("/blogs/{id}/input")
        public String editInput(@PathVariable Long id,Model model){
        setTypeAndTag(model);
        Blog blog = blogService.getBlog(id);
        blog.setType(typeService.getType(blog.getType_id()));
        blog.init();
        model.addAttribute("blogs",blog);
        return  INPUT;

        }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,Model model){

        blogService.DeleteBlogFK(id);
        blogService.DeleteBlog(id);

        return  REDIRECT_LIST;

    }

        @PostMapping("/blogs")
        public  String post(Blog blog, RedirectAttributes attributes, HttpSession session){

        if(blog.getId() == null)
        {
            User user = (User)session.getAttribute("user");
            blog.setUser_id(user.getId());
            blog.setType_id(blog.getType().getId());
            blog.setViews(0);
            blogService.saveBlog(blog);


        }else {
            User user = (User)session.getAttribute("user");
            blog.setUser_id(user.getId());
            blog.setType_id(blog.getType().getId());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
            blogService.EditBlog(blog);
        }





        return REDIRECT_LIST;
        }




}
